package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.JobseekerSocialQO;
import com.xingkeduo.pojo.table.JobseekerSocialTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.*;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.RegularUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 求职者个人信息封装类
 *
 * @author 高杰
 * @time 2018年12月25日 下午1:45:17
 * @description
 */
@Component
public class JobseekerSocialJTO extends BaseJson {
    public static JobseekerSocialTO createTableForNew(String json) throws Throwable {
        JobseekerSocialTO table = new JobseekerSocialTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setId(validId(root, YesNoEnum.YES)).setCode(validCode(root, YesNoEnum.YES))
                .setName(validName(root, YesNoEnum.YES)).analyzeIdCard(validIdCard(root, YesNoEnum.YES))
                .setPhone(validPhone(root, YesNoEnum.YES)).setEthnic(validEthnic(root, YesNoEnum.YES))
                .setDegree(validDegree(root, YesNoEnum.YES)).setIsMarried(validIsMarried(root, YesNoEnum.YES))
                .setHouseHold(validHouseHold(root, YesNoEnum.YES))
                .setHouseHoldType(validHouseHoldType(root, YesNoEnum.YES));

        return table;
    }

    public static JobseekerSocialTO createTableForModify(String json) throws Throwable {
        JobseekerSocialTO table = new JobseekerSocialTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setName(validName(root, YesNoEnum.NO)).analyzeIdCard(validIdCard(root, YesNoEnum.NO))
                .setPhone(validPhone(root, YesNoEnum.NO)).setEthnic(validEthnic(root, YesNoEnum.NO))
                .setDegree(validDegree(root, YesNoEnum.NO)).setIsMarried(validIsMarried(root, YesNoEnum.NO))
                .setHouseHold(validHouseHold(root, YesNoEnum.NO)).setBankCard(validBankCard(root, YesNoEnum.NO))
                .setHouseHoldType(validHouseHoldType(root, YesNoEnum.NO)).setBank(validBankValue(root, YesNoEnum.NO))
                .setBirth(validBirth(root, YesNoEnum.NO));
        return table;
    }

    public static JobseekerSocialQO createQueryForPrimary(String json) throws Throwable {
        JobseekerSocialQO query = new JobseekerSocialQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        query.setId(validId(root, YesNoEnum.YES));

        return query;
    }

    public static JobseekerSocialQO createQueryForList(String json) throws Throwable {
        JobseekerSocialQO query = new JobseekerSocialQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setName(validName(root, YesNoEnum.NO)).setPhone(validPhone(root, YesNoEnum.NO))
                .setNameOrPhone(validNameOrPhone(root, YesNoEnum.NO));

		query.setSex(validSex(root, YesNoEnum.NO));
        
        return query;
    }

    
    public static SexEnum validSex(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "sex";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!SexEnum.validValue(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return SexEnum.getSex(value);
		}
		return null;
	}
    
    public static Set<SexEnum> validSexs(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "sexs";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			Set<SexEnum> sexs = new HashSet<>();
			String value = node.path(key).asText().trim();
			String[] values = value.split(",");
			for (int i = 0; i < values.length; i++) {
				if (!SexEnum.validValue(values[i]))
					throw new InputParamException(InputParamException.Type.INVALID, key + " error");
				sexs.add(SexEnum.getSex(values[i]));
			}
			return sexs;
		}
		return null;
	}
    
    public static ObjectNode toJson(JobseekerSocialTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        SimpleDateFormat formatDatetime = null;

        if (table != null) {
            root.put("id", String.valueOf(table.getId())).put("code", String.valueOf(table.getCode()))
                    .put("name", table.getName()).put("idcard", table.getIdCard()).put("phone", table.getPhone());
            if (table.getIdCard() != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//                String sexStr = table.getIdCard().substring(16, 17);
//                table.setSex(SexEnum.getSex(Integer.parseInt(sexStr) % 2));
                String birthStr = table.getIdCard().substring(6, 10);
                String nowTime = sdf.format(new Date());
                root.put("age", Integer.valueOf(nowTime) - Integer.valueOf(birthStr));
            }
            if (table.getSex() != null) {
                root.put("sex_value", table.getSex().getValue()).put("sex_desc", table.getSex().getDesc());
            }
            if (table.getBirth() != null) {
                formatDatetime = new SimpleDateFormat("yyyy-MM-dd");
                root.put("birth", formatDatetime.format(table.getBirth()));
            }
            if (table.getEthnic() != null) {
                root.put("ethnic", table.getEthnic());
            }
            if (table.getDegree() != null) {
                root.put("degree_value", table.getDegree().getValue()).put("degree_desc", table.getDegree().getDesc());
            }
            if (table.getIsMarried() != null) {
                root.put("ismarried", table.getIsMarried().getValue());
            }
            root.put("household", table.getHouseHold());
            if (table.getHouseHoldType() != null) {
                root.put("householdtype_value", table.getHouseHoldType().getValue()).put("householdtype_desc",
                        table.getHouseHoldType().getDesc());
            }
            root.put("bank_value", "0");
            root.put("bank_desc", "-");
            if (table.getBank() != null) {
                root.put("bank_value", table.getBank().getValue());
                root.put("bank_desc", table.getBank().getDesc());
            }
            root.put("bankCard", "-");
            if (table.getBankCard() != null) {
                root.put("bankCard", table.getBankCard());
            }
            toBaseJson(root, table);
        }

        return root;
    }

    public static String validName(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "name";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 50)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    /**
     * 验证开户行的"-"
     *
     * @param node:
     * @param isRequired:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/12 13:58
     */
    public static BankEnums validBankValue(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "bank";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!BankEnums.validValue(value) ||value.length()>20)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return BankEnums.getBankValue(value);
        }
        return null;
    }
    /**
     * 验证生日
     *
     * @param node:
     * @param isRequired:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/12 13:58
     */
    public static Date validBirth(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "birth";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();

            if (CommonUtil.isEmpty(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            SimpleDateFormat formatDateTime = new SimpleDateFormat("yyyy-MM-dd");
            return formatDateTime.parse(value);
        }
        return null;
    }

    /**
     * 验证银行卡的"-"
     *
     * @param node:
     * @param isRequired:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/12 13:58
     */
    public static String validBankCard(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "bankCard";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);
        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            if (value.contains("-")) {
                return null;
            } else {
                return value;
            }
        }
        return null;
    }

    public static String validNameOrPhone(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "nameOrPhone";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (null == value || value.length() > 50)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validIdCard(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "idcard";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isDefaultIdCardNo(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validPhone(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "phone";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isSelfPhone(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validEthnic(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "ethnic";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static DegreeEnum validDegree(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "degree_value";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!DegreeEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return DegreeEnum.getDegree(value);
        }
        return null;
    }

    public static YesNoEnum validIsMarried(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "ismarried";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!YesNoEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return YesNoEnum.getYesNo(value);
        }
        return null;
    }

    public static String validHouseHold(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "household";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 30)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static HouseHoldTypeEnum validHouseHoldType(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "householdtype_value";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!HouseHoldTypeEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return HouseHoldTypeEnum.getHouseHoldType(value);
        }
        return null;
    }
}
